package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Zhanqu;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Zhanqu;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class Act_Zhanqu extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.listview)
	private NoScrollListView listview;
	@ViewInject(R.id.image)
	private ImageView image;
	
	private Adapter_Zhanqu adapter;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_zhanqu;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void init() {
		listview.setOnItemClickListener(this);
		adapter = new Adapter_Zhanqu(context);
		listview.setAdapter(adapter);
		getData();
	}
	
	private void getData(){
		Map<String, String> map = HttpUtil.getParams("app","promotion","act","exhibitor");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String s_title = jsonObject.getString("s_title");
				String s_logo = jsonObject.getString("s_logo");
				List<Zhanqu> list = JSON.parseArray(jsonObject.getString("s_contents"),Zhanqu.class);
				adapter.addAll(list);
				mAbTitleBar.setTitleText(s_title);
				ImageUtils.loadimg(image, s_logo, new OnImageLoadListener() {
					
					@Override
					public void onLoadSuccess(Drawable drawable) {
						int newWidth = ScreenUtil.getInstant().getDisWidth(context);
						int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
						LayoutParams layoutParams = (LayoutParams) image.getLayoutParams();
						layoutParams.width = newWidth;
						layoutParams.height = newHight;
						image.setLayoutParams(layoutParams);
					}
				});
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(this, Act_zhanqu_detail.class);
		intent.putExtra("data", adapter.getItem(position));
		startActivity(intent);
	}

}
